/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package input;

import java.io.File;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import toolz.JDirectoryScanner;
import DATA.iDataSetParts.SubstanceInfo;

/**
 *
 * @author Bunkowski
 */
public class IoToolbox {

    public static Vector<SubstanceInfo> decodeSubstanceInfos(String s) {
        Vector<SubstanceInfo> substances = new Vector<SubstanceInfo>();

        String[] newlinesplitted = s.split("\n");
        System.out.println(newlinesplitted.length + " lines splitted");
        for (String s2 : newlinesplitted) {
            substances.add(new SubstanceInfo(s2.split("\t")));
        }
        return substances;
    }

    public static SubstanceInfo decodeSubstanceInfo(String s) {
        return new SubstanceInfo(s.split("\t"));
    }

    public static Vector<File> decodeTabbedFileString(String s) {
        Vector<String> filepaths = toVector(s.split("\t"));
        Vector<File> filesToOpen = new Vector<File>();
        for (int i = 0; i < filepaths.size(); i++) {
            File file = new File(filepaths.get(i));
       //     if (file.exists()) {
                filesToOpen.addAll(JDirectoryScanner.scan(file));
       //     }
        }
        return filesToOpen;
    }

    public static Vector<String> toVector(File[] s) {
        Vector<String> v = new Vector<String>();
        for (File e : s) {
            v.add(e.toString());
        }
        return v;
    }

    public static Vector<String> toVector(String[] s) {
        Vector<String> v = new Vector<String>();
        for (String e : s) {
            v.add(e);
        }
        return v;
    }

    public static String[][] transformVectorArrayToArrayArray(Vector<String[]> vectorArray) {
        int maxLineLength = 0;
        for (String[] line : vectorArray) {
            if (line.length > maxLineLength) {
                maxLineLength = line.length;
            }
        }

        String[][] arrayArray = new String[vectorArray.size()][maxLineLength];

        for (int i = 0; i < vectorArray.size(); i++) {
            for (int j = 0; j < vectorArray.get(i).length; j++) {
                arrayArray[i][j] = vectorArray.get(i)[j];
            }
        }

        return arrayArray;
    }

    public static Vector<Vector<String>> transformVectorArrayToVectorVector(Vector<String[]> vectorArray) {
        Vector<Vector<String>> data = new Vector<Vector<String>>();

        for (int i = 0; i < vectorArray.size(); i++) {
            String[] strings = vectorArray.elementAt(i);
            Vector<String> line = new Vector<String>();
            for (int j = 0; j < strings.length; j++) {
                String string = strings[j];
                line.add(string);
            }
            data.add(line);
        }
        return data;
    }

    public static void removeSelectedRowsFromTable(JTable from) {
        int[] rows = from.getSelectedRows();
        TableModel tm = from.getModel();


        for (int row : rows) {
            ((DefaultTableModel) tm).removeRow(from.convertRowIndexToModel(row));
        }

        from.clearSelection();
    }
}
